package com.googlecode.lookie.platform.prefs;

import java.awt.BorderLayout;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import org.springframework.richclient.list.BeanPropertyValueListRenderer;
import org.springframework.richclient.list.ComboBoxListModel;
import org.springframework.richclient.preference.PreferencePage;

import com.googlecode.lookie.platform.settings.SettingsIds;
import com.googlecode.lookie.swing.laf.LookAndFeelDescriptor;
import com.googlecode.lookie.swing.laf.LookAndFeelRegistry;
import com.googlecode.lookie.swing.list.ComboSeparatorsRenderer;

public class LookAndFeelPreferencePage
    extends PreferencePage
{

    private LookAndFeelRegistry lookAndFeelRegistry;

    private JComboBox lookAndFeelComboBox;

    private ComboBoxListModel lookAndFeelComboBoxModel;

    public LookAndFeelPreferencePage()
    {
        super( "lookAndFeelPreferencePage" );
    }

    public void setLookAndFeelRegistry( LookAndFeelRegistry lookAndFeelRegistry )
    {
        this.lookAndFeelRegistry = lookAndFeelRegistry;
    }

    @Override
    protected JComponent createContents()
    {
        createLookAndFeelComboBox();
        final JLabel lookAndFeelComboBoxLabel = new JLabel( "Look And Feel" );
        LookAndFeelPreferencePanel panel = new LookAndFeelPreferencePanel( new LookAndFeelPreferencePanelParams()
        {

            public JComboBox getLookAndFeelComboBox()
            {
                return lookAndFeelComboBox;
            }

            public JLabel getLookAndFeelComboBoxLabel()
            {
                return lookAndFeelComboBoxLabel;
            }

        } );
        JPanel wrapper = getComponentFactory().createPanel( new BorderLayout() );
        wrapper.add( panel, BorderLayout.CENTER );

        onAboutToShow();

        return wrapper;
    }

    private void createLookAndFeelComboBox()
    {
        createLookAndFeelComboBoxModel();
        lookAndFeelComboBox = new JComboBox( lookAndFeelComboBoxModel );
        lookAndFeelComboBox.setRenderer( new ComboSeparatorsRenderer( new BeanPropertyValueListRenderer( "name" ) )
        {

            @Override
            protected boolean addSeparatorAfter( JList list, Object value, int index )
            {
                LookAndFeelDescriptor current = (LookAndFeelDescriptor) value;
                LookAndFeelDescriptor next = null;
                if ( lookAndFeelComboBoxModel.size() > index + 1 )
                {
                    next = (LookAndFeelDescriptor) lookAndFeelComboBoxModel.get( index + 1 );
                }
                return next != null && !current.getGroupId().equals( next.getGroupId() );
            }

        } );
    }

    private void createLookAndFeelComboBoxModel()
    {
        lookAndFeelComboBoxModel = new ComboBoxListModel();
        String[] groupIds = lookAndFeelRegistry.getGroupIds();
        for ( String groupId : groupIds )
        {
            for ( LookAndFeelDescriptor descriptor : lookAndFeelRegistry.getDescriptors( groupId ) )
            {
                lookAndFeelComboBoxModel.add( descriptor );
            }
        }
    }

    @Override
    protected void onDefaults()
    {
        String id = getSettings().getDefaultString( SettingsIds.LAF );
        LookAndFeelDescriptor descriptor = lookAndFeelRegistry.getDescriptor( id );
        lookAndFeelComboBoxModel.setSelectedItem( descriptor );
    }

    @Override
    protected boolean onFinish()
    {
        LookAndFeelDescriptor selected = (LookAndFeelDescriptor) lookAndFeelComboBoxModel.getSelectedItem();
        getSettings().setString( SettingsIds.LAF, selected.getId() );
        return super.onFinish();
    }

    public void onAboutToShow()
    {
        String currentId = getSettings().getString( SettingsIds.LAF );
        LookAndFeelDescriptor currentDescriptor = lookAndFeelRegistry.getDescriptor( currentId );
        lookAndFeelComboBoxModel.setSelectedItem( currentDescriptor );
    }

}
